using System;
using System.Collections.Generic;
using System.Linq;

namespace Hive.Core
{
    public class GameState
    {
        public List<Location> MyAnts { get; private set; }
        public List<AntHillLocation> MyHills { get; private set; }
        public List<AntLocation> EnemyAnts { get; private set; }
        public List<AntHillLocation> EnemyHills { get; private set; }
        public List<Location> DeadTiles { get; private set; }
        public List<Location> FoodTiles { get; private set; }

        public int TurnNumber { get; set; }
        
        public GameState()
        {
            MyAnts = new List<Location>();
            MyHills = new List<AntHillLocation>();
            EnemyAnts = new List<AntLocation>();
            EnemyHills = new List<AntHillLocation>();
            DeadTiles = new List<Location>();
            FoodTiles = new List<Location>();
        }

        #region State mutators
        static int CurrentTurn = 0;
        public void StartNewTurn()
        {
            CurrentTurn++;
            TurnNumber = CurrentTurn;
        }

        public void AddAnt(int row, int col, int team)
        {
            if (team == 0)
            {
                MyAnts.Add(new Location(row, col));
            }
            else
            {
                AntLocation ant = new AntLocation(row, col, team);
                EnemyAnts.Add(ant);
            }
        }

        public void AddFood(int row, int col)
        {
            FoodTiles.Add(new Location(row, col));
        }

        public void RemoveFood(int row, int col)
        {
            FoodTiles.Remove(new Location(row, col));
        }

        public void DeadAnt(int row, int col)
        {
            // but always add to the dead list
            DeadTiles.Add(new Location(row, col));
        }

        public void AntHill(int row, int col, int team)
        {
            AntHillLocation hill = new AntHillLocation(row, col, team);
            if (team == 0)
                MyHills.Add(hill);
            else
                EnemyHills.Add(hill);
        }
        #endregion

        public bool GetIsVisible(Location loc)
        {
            List<Location> offsets = Env.GetVisibilityOffsets();
            foreach (Location ant in this.MyAnts)
            {
                foreach (Location offset in offsets)
                {
                    if ((ant.Col + offset.Col) == loc.Col &&
                        (ant.Row + offset.Row) == loc.Row)
                    {
                        return true;
                    }
                }
            }
            return false;
        }

    }
}

